package com.zeildlin.seecode.Controller;

import com.zeildlin.seecode.Service.DiscussPostService;
import com.zeildlin.seecode.Service.LikeSerivce;
import com.zeildlin.seecode.Service.UserService;
import com.zeildlin.seecode.entity.DiscussPost;
import com.zeildlin.seecode.entity.Page;
import com.zeildlin.seecode.entity.User;
import com.zeildlin.seecode.util.CommunitConstant;
import com.zeildlin.seecode.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class HomeController implements CommunitConstant {
    @Autowired
    DiscussPostService discussPostService;
    @Autowired
    UserService userService;

    @Autowired
    LikeSerivce likeSerivce;

    @RequestMapping(path = "/index",method = RequestMethod.GET)
    public String getHomePage(Model model, Page page){

        page.setRows(discussPostService.selectDiscussPostRows(0));
        page.setPath("/index");

//        PageHelper.startPage(pageNum,pageSize);
        List<Map<String,Object>> discussPosts = new ArrayList<>();
        List<DiscussPost>list = discussPostService.findDiscussPosts(0,page.getOffset(),page.getLimit());




        for(DiscussPost discussPost:list){
            Map<String,Object>map = new HashMap<>();
            map.put("post",discussPost);
            User user = userService.FindUserById(discussPost.getUserId());
            map.put("user",user);
            discussPosts.add(map);
            long likeCount = likeSerivce.findEntityLikeCount(ENTITY_TYPE_POST,discussPost.getId());
            map.put("likeCount",likeCount);
        }

//        PageInfo<DiscussPost> postPageInfo = new PageInfo<>(list);
//        model.addAttribute("page",postPageInfo);

        model.addAttribute("discussPosts",discussPosts);
        return "index";
    }
}
